﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Sistema_de_Gestión_de_Correspondencia.Models;

namespace Sistema_de_Gestión_de_Correspondencia.Models.Listas
{
    public class ListaPaginada<T> : List<T>
    {
        public int PageIndex { get; private set; }
        public int PageSize { get; private set; }
        public int TotalCount { get; private set; }
        public int TotalPages { get; private set; }
        public List<SelectListItem> listaDimensionPaginas { get; private set; }
        
        public ListaPaginada(List<T> source, int pageIndex, int pageSize)
        {            
            PageIndex = pageIndex;
            PageSize = pageSize;
            TotalCount = source.Count();
            TotalPages = (int)Math.Ceiling(TotalCount / (double)PageSize);
            this.AddRange(source.Skip(PageIndex * PageSize).Take(PageSize));            
            crearListaDimensionPaginas();

        }

        public bool HasPreviousPage
        {
            get
            {
                return (PageIndex > 0);
            }
        }

        public bool HasNextPage
        {
            get
            {
                return (PageIndex + 1 < TotalPages);
            }
        }

        public void crearListaDimensionPaginas()
        {
            listaDimensionPaginas = new List<SelectListItem>();
            listaDimensionPaginas.Add(new SelectListItem { Text = "5", Value = "5" });
            listaDimensionPaginas.Add(new SelectListItem { Text = "10", Value = "10" });
            listaDimensionPaginas.Add(new SelectListItem { Text = "15", Value = "15" });
            listaDimensionPaginas.Add(new SelectListItem { Text = "20", Value = "20" });
            listaDimensionPaginas.Add(new SelectListItem { Text = "25", Value = "25", Selected = true });
            listaDimensionPaginas.Add(new SelectListItem { Text = "30", Value = "30" });
            listaDimensionPaginas.Add(new SelectListItem { Text = "50", Value = "50" });
            listaDimensionPaginas.Add(new SelectListItem { Text = "100", Value = "100"});
            listaDimensionPaginas.Add(new SelectListItem { Text = "Todos", Value = TotalCount.ToString() });
        }
    }
}